package com.celesea.framework.util;

import com.google.common.collect.Lists;

import java.io.*;
import java.util.Collection;
import java.util.List;

public class CollectionKit {

    public static boolean isEmpty(Collection<?> c) {
        return c == null || c.isEmpty();
    }

    /**
     * @param c
     * @return
     */
    public static boolean isNotEmpty(Collection<?> c) {
        return !isEmpty(c);
    }

    /**
     * @param arrays
     * @return
     */
    @SafeVarargs
    public static <T extends Object> boolean isEmptyArray(T... arrays) {
        return arrays == null || arrays.length == 0;
    }

    /**
     * @param arrays
     * @return
     */
    @SafeVarargs
    public static <T extends Object> boolean isNotEmptyArray(T... arrays) {
        return !isEmptyArray(arrays);
    }

    /**
     * @param listsParam
     * @param ts
     * @return
     */
    public static <T> List<T> addArray(List<T> listsParam, T[] ts) {
        List<T> lists = Lists.newArrayList();
        if (ts == null || ts.length == 0) {
            return lists;
        }
        if (listsParam == null) {
            lists = Lists.newArrayList();
        } else {
            lists = listsParam;
        }
        for (T t : ts) {
            lists.add(t);
        }
        return lists;
    }

    /**
     * @param src
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     * @Description: 集合深拷贝
     */
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream input = new ObjectInputStream(byteIn);
        return (List<T>) input.readObject();
    }

    public static <T> T firstNotEmpty(T... params) {
        if (CollectionKit.isEmptyArray(params)) {
            return null;
        }
        for (T t : params) {
            if (t != null) {
                return t;
            }
        }
        return null;
    }

}
